<?php

class Gallery_IndexController extends Zend_Controller_Action {

	public function indexAction() {
		$photos = Doctrine_Query::create()
				  ->from('Gallery_Model_Photo')
				  ->where('published = ', true)
				  ->orderBy('ordering ASC')
				  ->addOrderBy('id DESC')
				  ->execute();
		//устананавливаем метатеги
		$siteNamespace = new Zend_Session_Namespace('site');
		
		$setting = Doctrine_Core::getTable('Default_Model_Settings')->find(1);
		$this->view->headTitle($this->view->translate('Gallery'));
		if($setting) {
			$this->view->headMeta()->appendName('description', $setting->Translation[$siteNamespace->lang]->seo_description);
			$this->view->headMeta()->appendName('keywords', $setting->Translation[$siteNamespace->lang]->seo_keywords);
		}
		$this->view->photos = $photos;
	}
	
	public function getimgAction() {
		if($this->_request->isXmlHttpRequest()) {
			// Отключаем авторендеринг шаблонов
			$this->_helper->viewRenderer->setNoRender();
			// Отключаем шаблоны
			$this->_helper->layout()->disableLayout();
		
			$id = $this->_request->getParam('id',0);
			
			
			$photo = Doctrine_Core::getTable('Gallery_Model_Photo')->find($id);
			
			if(!$id || !$photo)
				throw new Zend_Controller_Action_Exception('Photo not found!', 404);
			
			$imgSize = getimagesize($this->view->STATIC_SERVER . '/reflect3.php?img=upload/media/pictures/gallery/medium/' . $photo->photo . '&height=33%');
			
			$response_array = array(
				'src' => $photo->photo,
				'width' => $imgSize[0],
				'height' => $imgSize[1],
				'name' => $photo->Translation[$this->view->LANG]->name
			);	
			
			$response = $this->getResponse();
			$response->appendBody(Zend_Json::encode($response_array))->setHeader('content-type', 'text/plain', true);
			
		} else {
			return $this->_redirect($this->view->url(array(), 'gallery_index_index'));
		}
	}
}